//
//  CALayer+Extension.swift
//  KeyboardAndEmoji
//
//  Created by AP020Mac mini on 30-07-25.
//

import UIKit
/// 渐变方向类型
public enum GradualDirectionType: Int ,Codable{
    /// 从左到右
    case leftToRight
    /// 从右到左
    case rightToLeft
    /// 从上到下
    case topToBottom
    /// 从左上到右下
    case leftTopToRightBottom
    /// 从左下到右上
    case leftBottomToRightTop
    /// 从顶部到右下
    case topToRightBottom
}

extension CALayer {
    /// 配置渐变 Layer
    /// - Parameters:
    ///   - size: 渐变层的尺寸
    ///   - gradientColors: 渐变颜色数组
    ///   - directionType: 渐变方向
    /// - Returns: 配置好的 CAGradientLayer
    static func configGradientLayer(size: CGSize, gradientColors: [UIColor], directionType: GradualDirectionType) -> CAGradientLayer {
        let gradientLayer = CAGradientLayer()
        // 根据方向设置起点和终点
        switch directionType {
        case .leftToRight:
            gradientLayer.startPoint = CGPoint(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        case .rightToLeft:
            gradientLayer.startPoint = CGPoint(x: 1, y: 0)
            gradientLayer.endPoint = CGPoint(x: 0, y: 0)
        case .topToBottom:
            gradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
            gradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
        case .leftTopToRightBottom:
            gradientLayer.startPoint = CGPoint(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint(x: 1, y: 1)
        case .leftBottomToRightTop:
            gradientLayer.startPoint = CGPoint(x: 0, y: 1)
            gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        case .topToRightBottom:
            gradientLayer.startPoint = CGPoint(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        }
        let cgColors = gradientColors.isEmpty ? [UIColor.white.cgColor, UIColor.white.cgColor] : gradientColors.map { $0.cgColor }
        gradientLayer.colors = cgColors
        gradientLayer.frame = CGRect(origin: .zero, size: size)
        let space = 1.0 / CGFloat(max(cgColors.count - 1, 1))
        gradientLayer.locations = (0..<cgColors.count).map { NSNumber(value: Float($0) * Float(space)) }
        return gradientLayer
    }

    /// 生成渐变边框
    /// - Parameters:
    ///   - size: 边框的整体尺寸
    ///   - cornerRadius: 圆角半径
    ///   - borderWidth: 边框宽度
    ///   - borderColors: 边框渐变颜色数组
    ///   - directionType: 渐变方向
    /// - Returns: 带有渐变色的边框 CAGradientLayer
    static func getGradientBorder(size: CGSize, cornerRadius: CGFloat, borderWidth: CGFloat, borderColors: [UIColor], directionType: GradualDirectionType) -> CAGradientLayer {
        let mapRect = CGRect(x: borderWidth / 2, y: borderWidth / 2, width: size.width - borderWidth, height: size.height - borderWidth)
        let gradientLayer = CAGradientLayer.configGradientLayer(size: size, gradientColors: borderColors, directionType: directionType)
        gradientLayer.cornerRadius = cornerRadius
        let maskLayer = CAShapeLayer()
        maskLayer.lineWidth = borderWidth
        let path = UIBezierPath(roundedRect: mapRect, cornerRadius: cornerRadius)
        maskLayer.path = path.cgPath
        maskLayer.fillColor = UIColor.clear.cgColor
        maskLayer.strokeColor = UIColor.blue.cgColor
        gradientLayer.mask = maskLayer
        return gradientLayer
    }
    
    static func createDashedBorderLayer( color: UIColor,viewSize:CGSize = .zero, cornerRadius: CGFloat,
        lineWidth: CGFloat = 1, dashPattern: [NSNumber] = [4, 2]
    ) -> CALayer {
        let borderLayer = CAShapeLayer()
        let tempBounds = CGRect(x: 0, y: 0, width: viewSize.width, height: viewSize.height)
        // 创建矩形路径
        let path = UIBezierPath(
            roundedRect: tempBounds,
            cornerRadius: cornerRadius
        )
        
        // 配置虚线图层
        borderLayer.path = path.cgPath
        borderLayer.strokeColor = color.cgColor
        borderLayer.lineWidth = lineWidth
        borderLayer.lineDashPattern = dashPattern
        borderLayer.fillColor = nil // 透明填充
        borderLayer.frame = tempBounds
        return borderLayer
    }
    
    
    
//    static func tabbarApplyCornerRadiusAndShadow(frame: CGRect ,cornerRadius: CGFloat, shadowColor: UIColor = .black) -> CAShapeLayer {
//          
//          // 创建一个形状图层
//          let shapeLayer = CAShapeLayer()
//          shapeLayer.path = UIBezierPath(roundedRect: frame, cornerRadius: cornerRadius).cgPath
//          shapeLayer.shadowColor = shadowColor.cgColor
//          shapeLayer.shadowOpacity = 1
//          shapeLayer.shadowOffset = .zero
//          shapeLayer.shadowRadius = 3
//        shapeLayer.backgroundColor = .red.cgColor
//          shapeLayer.fillColor = ColorWhite.cgColor
//          shapeLayer.strokeColor = ColorWhite.cgColor
//          return shapeLayer

//      }
   
}
